"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/3/14 14:08
@Description : 
   设计模式:  代理模式  实现不修改源代码  实现功能的增强
   1.代理类要依赖于被代理类
   2.代理类和被代理类要有相同的父类
"""
from abc import ABC, abstractmethod
import time


class Image(ABC):
    @abstractmethod
    def display(self):
        pass


class RealImage(Image):
    def __init__(self, filename):
        self.filename = filename
        self.load_from_disk()

    def load_from_disk(self):
        print(f'正在从磁盘中加载{self.filename}图片...')

    def display(self):
        print(f'正在展示{self.filename}图片...')


# 创建Image类型实例
# img1: Image = RealImage('test.jpg')
# img1.display()


# 创建代理类     被代理类 RealImage
class ProxyImage(Image):

    def __init__(self, filename):
        self.filename = filename
        self.real_image = None

    def display(self):
        if self.real_image is None:
            self.real_image = RealImage(self.filename)
        print(f'正在展示{self.filename}图片...')


# img2: Image = ProxyImage('test.jpg')
# img2.display()
# img2.display()


class Calculator(ABC):

    @abstractmethod
    def add(self, a, b):
        pass


# 被代理类
class CalculatorA(Calculator):

    def add(self, a, b):
        return a + b


calc: Calculator = CalculatorA()
# print(calc.add(1, 2))


# 代理类
class ProxyCalculator(Calculator):

    def __init__(self):
        self.obj = CalculatorA()

    def add(self, a, b):
        start = time.time()
        # 调用被代理类对象的add  实现基础功能引入
        resut = self.obj.add(a, b)
        end = time.time()
        print(f'计算完毕,耗时{end-start}s')
        return  resut

calc: Calculator = ProxyCalculator()
print(calc.add(1, 2))